package com.software.modelcenter.controller;

import com.software.modelcenter.entity.ModelMine;
import com.software.modelcenter.service.ModelMineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 *
 */

@RestController
@RequestMapping("/modelmine")
public class ModelMineController {

    private static String path = "model/";

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private ModelMineService modelMineService;

    @GetMapping("/getListModelMineById")
    public List<ModelMine> getListModelMineById(@RequestParam("userId")String userId){
        List<ModelMine> modelMines = modelMineService.ListModelMineById(userId);
        return modelMines;
    }

    @PostMapping("/download")
    public String fileDownLoad(HttpServletResponse response, @RequestParam("modelId") String fileName){
        File file = new File(path + fileName);
        if(!file.exists()){
            return "下载文件不存在";
        }
        response.reset();
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName );

        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));) {
            byte[] buff = new byte[1024];
            OutputStream os  = response.getOutputStream();
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            return "下载失败";
        }
        return "下载成功";
    }

    @PostMapping("/updateModelMine")
    public ModelMine updateModelMine(ModelMine modelMine){
        System.out.println(modelMine);
        ModelMine save = modelMineService.updateModelMine(modelMine);
        return save;
    }

    @GetMapping("/deleteModelMine")
    public String deleteModelMine(@RequestParam("modelId") String modelId){
        modelMineService.deleteModelMineByModelId(path + modelId);
        return "删除成功";
    }

    @GetMapping(value = "/images/{imageName}", produces = MediaType.IMAGE_JPEG_VALUE)
    public InputStreamResource getImage(@PathVariable String imageName) throws IOException {
//        Resource resource = new ClassPathResource("static/images/" + imageName + ".jpg");
        Resource resource = resourceLoader.getResource("file:model_img/" + imageName);

        // 检查资源是否存在
        if (resource.exists()) {
            InputStream inputStream = resource.getInputStream();
            return new InputStreamResource(inputStream);
        } else {
            throw new RuntimeException("Image not found: " + imageName);
        }
    }
}
